clear
clear mata
clear matrix
set maxvar 10000
estimates clear


loc didopt "autosample horizons(0/1) cluster(hhidpn)"
loc estout "cells(b(fmt(3) star) se(par fmt(3))) mlabels(none) la collabels(,none) eqlabels(none) varwidth(16) modelwidth(12) style(tex) starl(* 0.05 ** .01) rep" 
loc stats_estout "stats( Observations Households, fmt(0 0)) cells(b(fmt(3) star) se(par fmt(3))) mlabels(none) la collabels(,none) eqlabels(none) varwidth(16) modelwidth(12) style(tex) starl(* 0.05 ** .01) rep" 

use data/constructed_data/analysisdata.dta

foreach event in wneg2 any_rlost_job any_rhosp any_disab any_health3to5 {
di "`event'"
preserve
gen temp = 1 if `event'==1 & l.`event'==0 & l2.`event'==0 & any_rwidowed==0 
bys hhidpn: egen firstevent = min(wave*temp)
drop temp
gen eventtime = wave - firstevent 
gen eventever = firstevent~=.
keep if eventever==1  

eststo bat_pk_`event': did_imputation at_pk hhidpn wave firstevent [w=weight], `didopt' saveweights
rename __* myweights* 
eststo bat_kp_`event': did_imputation at_kp hhidpn wave firstevent [w=weight], `didopt' loadweights(myweights*)
eststo bany_helpr_`event': did_imputation any_helpr hhidpn wave firstevent [w=weight], `didopt' loadweights(myweights*)
restore
}

foreach event in male_rwidowed fem_rwidowed {
di "`event'"
preserve
gen temp = 1 if `event'==1 & l.`event'==0 & l2.`event'==0 
bys hhidpn: egen firstevent = min(wave*temp)
drop temp
gen eventtime = wave - firstevent 
gen eventever = firstevent~=.
keep if eventever==1  

eststo bat_pk_`event': did_imputation at_pk hhidpn wave firstevent [w=weight], `didopt' saveweights
rename __* myweights* 
eststo bat_kp_`event': did_imputation at_kp hhidpn wave firstevent [w=weight], `didopt' loadweights(myweights*)
eststo bany_helpr_`event': did_imputation any_helpr hhidpn wave firstevent [w=weight], `didopt' loadweights(myweights*)
restore
}

foreach lhs in at_pk at_kp any_helpr {
estout b`lhs'_wneg2 b`lhs'_any_rlost_job  b`lhs'_male_rwidowed b`lhs'_fem_rwidowed b`lhs'_any_rhosp b`lhs'_any_disab b`lhs'_any_health3to5 using output/tablec6/`lhs'_bor.tex, `estout'
filefilter output/tablec6/`lhs'_bor.tex output/tablec6/`lhs'_bor2.tex, from("tau0") to ("Event Wave") replace
filefilter output/tablec6/`lhs'_bor2.tex output/tablec6/`lhs'_bor3.tex, from("tau1") to ("One Wave After") replace
}




loc didopt "autosample horizons(0/4) pretrends(4) cluster(hhidpn)"

use data/constructed_data/analysisdata.dta

foreach event in wneg2 any_rlost_job any_rhosp any_disab any_health3to5 {

di "`event'"
preserve

//Setup
gen temp = 1 if `event'==1 & l.`event'==0 & l2.`event'==0 & any_rwidowed==0 
bys hhidpn: egen firstevent = min(wave*temp)
drop temp
gen eventtime = wave - firstevent 
gen eventever = firstevent~=.
keep if eventever==1  

did_imputation at_pk hhidpn wave firstevent  [w=weight], `didopt' saveweights
rename __* myweights* 
event_plot, default_look
graph export "output/figurec123/`event'_at_pk_bor.pdf", replace


did_imputation at_kp hhidpn wave firstevent [w=weight], `didopt' loadweights(myweights*)
event_plot, default_look
graph export "output/figurec123/`event'_at_kp_bor.pdf", replace


did_imputation any_helpr hhidpn wave firstevent [w=weight], `didopt' loadweights(myweights*)
event_plot, default_look
graph export "output/figurec123/`event'_any_helpr_bor.pdf", replace

restore
}

foreach event in male_rwidowed fem_rwidowed {

di "`event'"
preserve

//Setup
gen temp = 1 if `event'==1 & l.`event'==0 & l2.`event'==0 
bys hhidpn: egen firstevent = min(wave*temp)
drop temp
gen eventtime = wave - firstevent 
gen eventever = firstevent~=.
keep if eventever==1  

did_imputation at_pk hhidpn wave firstevent [w=weight], `didopt' saveweights
rename __* myweights* 
event_plot, default_look
graph export "output/figurec123/`event'_at_pk_bor.pdf", replace


did_imputation at_kp hhidpn wave firstevent [w=weight], `didopt' loadweights(myweights*)
event_plot, default_look
graph export "output/figurec123/`event'_at_kp_bor.pdf", replace


did_imputation any_helpr hhidpn wave firstevent [w=weight], `didopt' loadweights(myweights*)
event_plot, default_look
graph export "output/figurec123/`event'_any_helpr_bor.pdf", replace

restore
}
